import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core';
import { AutopartsService } from '@core/services/autoparts/autoparts.service';
import { ApiService } from '@core/services/http/api.service';
import { Subscription } from 'rxjs';
import { ApesMessageService } from '@shared/apes/message';
import { AutopartsViewService } from '@core/services/autoparts/autoparts.view.service';

/***
 * 目录清单
 */

@Component({
  selector: 'epc-menu',
  templateUrl: './epc-menu.component.html',
  styleUrls: ['./epc-menu.component.scss']
})
export class EpcMenuComponent implements OnInit, OnDestroy {

  @Output() selectedMenu = new EventEmitter();
  data = {};
  sourceData:any = {}; // 原始数据，记录类型
  dataSource: 'EPC' | 'OWNER' = null; //展示类型

  isLoading: boolean = false;

  subscription: Subscription;

  constructor(
    private apiService: ApiService,
    private autopartsViewService: AutopartsViewService,
    private autopartsService : AutopartsService
  ) {
  }

  ngOnInit(): void {
    this.subscription = this.autopartsViewService.menuChange$.subscribe((cataLog:any) => {
      this.isLoading = false;
      if( cataLog && cataLog.dataSource ) {
        this.dataSource = cataLog.dataSource;
        this.data = cataLog.data;
        this.sourceData = cataLog;
      } else {
        this.dataSource = null;
        this.data = {};
        this.sourceData = {};
      }
    });
    this.autopartsViewService.subgroupChange$.subscribe(() => this.isLoading = false);
  }

  ngOnDestroy(): void {
    this.subscription.unsubscribe();
  }

  onSelect(node) {
    console.log('当前选中的数据为，', node);
    this.isLoading = true;
    this.autopartsService.selectMenu$.next({...node, dataSource: this.sourceData.dataSource});
  }

  switchMenuModel(dataSource) {
    if( dataSource === 'OWNER' && this.dataSource === 'OWNER' ) return;
    if( dataSource === 'EPC' && this.dataSource === 'EPC' ) return;
    this.autopartsService.switchMenuModel({dataSource});
  }

}
